<?php
/**
 * @Author: Xu Liu email:2867557054@qq.com
 * @Date:   2016-12-15 13:31:33
 * @Last Modified by:   Xu Liu
 * @Last Modified time: 2018-01-03 23:04:36
 */
if (!defined('IN_DISCUZ')) {
    exit('Access Denid');
}
class table_jamesonread_scores extends discuz_table
{

    public function __construct()
    {
        $this->_table         = 'jamesonread_scores';
        $this->_pk            = 'score_id';
        $this->_pre_cache_key = 'jamesonreadscores_';
        $this->_cache_ttl     = 300;
        parent::__construct();
    }
    /*是否已经有过评分*/
    public function hasScore($uid, $book_id)
    {
        return DB::result_first('SELECT count(score_id) FROM %t WHERE uid=%d AND book_id=%d', array($this->_table, $uid, $book_id));
    }
    public function fetchScore($uid, $book_id)
    {
        return DB::fetch_first('SELECT * FROM %t WHERE uid=%d AND book_id=%d', array($this->_table, $uid, $book_id));
    }
    // 插入数据同时更新books表
    public function insertAndBooks($data)
    {
        $res1 = $this->insert($data, true);
        $res2 = DB::query('UPDATE ' . DB::table('jamesonread_books') . ' SET ' . DB::field('scores', $data['score'], '+') . ' WHERE ' . DB::field('book_id', $data['book_id']));
        $res3 = DB::query('UPDATE ' . DB::table('jamesonread_books') . ' SET ' . DB::field('dpcount', '1', '+') . ' WHERE ' . DB::field('book_id', $data['book_id']));
        return $res1 && $res2 && $res3;
    }
}